#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import os
from enum import Enum

AGENT_HOME = os.getenv("DATA_BACKUP_AGENT_HOME", None)
os_join = os.path.join


class SAPOracleConsts:
    BRTOOLS_LOCATION = r"C:\usr\sap\{sapsid}\SYS\exe\run"

    DB_PLUGIN_PATH = fr"{AGENT_HOME}\DataBackup\ProtectClient\Plugins\GeneralDBPlugin"
    LOG_PATH = fr"{AGENT_HOME}\DataBackup\ProtectClient\ProtectClient-E\log\Plugins\GeneralDBPlugin"

    BACKINT_PATH = os.path.join(DB_PLUGIN_PATH, "bin", "backint")

    TMP_PATH = os.path.join(DB_PLUGIN_PATH, "tmp")
    STMP_PATH = os.path.join(DB_PLUGIN_PATH, "stmp")

    BRTOOLS_LOG = "saporacle_brtools.log"

    """运行时"""

    BACKINT_PARAM_FILE = "{oracle_sid}_backint_param.json"
    BRTOOLS_OUTFILE = "{oracle_sid}_brtools_running.txt"
    RESTORE_TMP_DATAPATH = "{oracle_sid}_restore_data"


class SAPOracleFile:
    BRBACKUP_DETAIL_LOG = r"^.+\.anf$"
    BRBACKUP_SUMMARY_LOG = r"^back.+\.log$"
    BRARCHIVE_DETAIL_LOG = r"^.+\.(sve|cps)$"
    BRARCHIVE_SUMMARY_LOG = r"^arch.+\.log$"
    CONTROL_FILE = r"^cntrl.+\.dbf$"
    SPFILE = r"^spfile.+\.ora$"
    ORACLE_PROFILE = r"^init.+\.ora$"
    SAP_PROFILE = r"^init.+\.sap$"
    SPACE_LOG = r"^space.+\.log$"


class SAPOracleFileStructure:
    """Oracle Home Directory"""
    ORACLE_DATABASE = os_join("{ORACLE_HOME}", "database")

    """SAP Home Directory"""
    SAPARCH = os.getenv("SAPARCH") or os_join("{SAPDATA_HOME}", "saparch")
    SAPBACKUP = os.getenv("SAPBACKUP") or os_join("{SAPDATA_HOME}", "sapbackup")
    SAPCHECK = os.getenv("SAPCHECK") or os_join("{SAPDATA_HOME}", "sapcheck")
    SAPREORG = os.getenv("SAPREORG") or os_join("{SAPDATA_HOME}", "sapreorg")
    SAPTRACE = os.getenv("SAPTRACE") or os_join("{SAPDATA_HOME}", "saptrace")
    SAPDATA1 = os.getenv("SAPDATA1") or os_join("{SAPDATA_HOME}", "sapdata1")
    SAPDATA2 = os.getenv("SAPDATA2") or os_join("{SAPDATA_HOME}", "sapdata2")
    ORIGLOGA = os_join("{SAPDATA_HOME}", "origlogA")
    ORIGLOGB = os_join("{SAPDATA_HOME}", "origlogB")

    """File"""
    ORACLE_SPFILE = os_join(ORACLE_DATABASE, "spfile{ORACLE_SID}.ora")
    ORACLE_PROFILE = os_join(ORACLE_DATABASE, "init{ORACLE_SID}.ora")
    BRTOOLS_PROFILE = os_join(ORACLE_DATABASE, "init{ORACLE_SID}.sap")
    CONTROL_FILES = [os_join(SAPDATA1, "cntrl", "cntrl{ORACLE_SID}.dbf"),
                     os_join(SAPDATA2, "cntrl", "cntrl{ORACLE_SID}.dbf"),
                     os_join(ORIGLOGA, "cntrl", "cntrl{ORACLE_SID}.dbf"),
                     os_join(ORIGLOGB, "cntrl", "cntrl{ORACLE_SID}.dbf")]
    BRARCHIVE_SUMMARY_LOG = os_join(SAPARCH, "arch{ORACLE_SID}.log")
    BRBACKUP_SUMMARY_LOG = os_join(SAPARCH, "back{ORACLE_SID}.log")
    BRSPACE_SUMMARY_LOG = os_join(SAPARCH, "space{ORACLE_SID}.log")
    BRBACKUP_SUMMARY_LOG_FILE = "back{ORACLE_SID}.log"
    BRARCHIVE_SUMMARY_LOG_FILE = "arch{ORACLE_SID}.log"
    BRSPACE_SUMMARY_LOG_FILE = "space{ORACLE_SID}.log"
    CONTROL_FILES_WITH_FILE_NAME = "cntrl{ORACLE_SID}.dbf"
    BRTOOLS_PROFILE_FILE = "init{ORACLE_SID}.sap"
    ORACLE_PROFILE_FILE = "init{ORACLE_SID}.ora"
    ORACLE_SPFILE_FILE = "spfile{ORACLE_SID}.ora"


class SAPOracleErrorCode(int, Enum):
    """Agent"""
    # 操作失败。
    RPCTOOL_FAILED = 1677929219

    """saporacle 错误码"""
    # 操作失败。
    UNKNOWN_ERROR = 1677929219
    # brtools 缺失
    BRTOOLS_NOT_EXIST = 1577213588
    # 安装 backint 失败
    INSTALL_BACKINT_FAILED = 1577213515
    # 数据库未关闭
    DATABASE_ONLINE = 1577209863
    # 环境变量 {1} 未设置
    ENVIRONMENTS_NOT_SET = 1577213589
    # 执行命令（{0}）异常({1})。
    SUBPROCESS_EXCEPTION = 1577209989
    # 生产环境没有新的日志副本。
    NO_NEW_LOG = 1577213485
    # 操作失败。
    BACKUP_COPY_DAMAGED = 1677929219
    # 内部错误
    ORACLE_ERROR = 1593988923
    # 生产环境上日志不连续。
    LOG_DISCONTINUOUS = 1577213480

    """复用 oracle"""
    # 数据库用户名或密码错误 未找到 omrp 错误码
    DB_USERPWD_WRONG = 1577209986
    # 数据库内部错误 0x5F02573B
    INTERNAL_ERROR = 1593988923
    # 数据库离线
    DATABASE_OFFLINE = 1577210050
    # 数据库用户权限不足 未找到 omrp 错误码
    INSUFFICIENT_USER_PRIVILEGE = 0x4003291E
    # 数据库连接已超过最大连接数
    ERROR_ORACLE_OVER_MAX_LINK = 1577211394


class RPCConstants:
    RPC_TOOL_PATH = fr"{AGENT_HOME}\DataBackup\ProtectClient\Plugins\GeneralDBPlugin\bin\rpctool.bat"
    PARAM_FILE_PREFIX = "rpcInput_"
    RESULT_FILE_PREFIX = "rpcOutput_"


class SAPOracleJobLabel(str, Enum):
    # "数据保护代理主机（{0}）正在执行备份子任务，待备份的数据量：（{1}），已备份的数据量：（{2}）。"
    BACKUP_RUNNING_LABEL = "saporacle_backup_running_label"
    # "数据保护代理主机（{0}）正在执行恢复子任务，待恢复的数据量：（{1}），已恢复的数据量：（{2}）。"
    RESTORE_RUNNING_LABEL = "saporacle_restore_running_label"



